package com.hyjiacan.apps.audionly.data.repository

import com.hyjiacan.apps.audionly.data.model.AudioParam
import com.hyjiacan.apps.audionly.data.model.TaskInfo
import com.hyjiacan.apps.audionly.data.model.VideoFile
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import kotlinx.coroutines.runBlocking
import java.util.Date
import java.util.UUID

/**
 * TaskRepositoryImpl类的单元测试
 */
class TaskRepositoryImplTest {
    private val repository = TaskRepositoryImpl()

    @Test
    fun `test insertTask`() = runBlocking {
        // 创建测试任务
        val taskInfo = createTestTaskInfo()

        // 测试插入任务
        val result = repository.insertTask(taskInfo)
        assertTrue(result)

        // 验证任务已插入
        val insertedTask = repository.getTaskById(taskInfo.id)
        assertEquals(taskInfo, insertedTask)

        // 测试插入重复ID的任务
        val duplicateResult = repository.insertTask(taskInfo)
        assertFalse(duplicateResult)
    }

    @Test
    fun `test updateTask`() = runBlocking {
        // 创建并插入测试任务
        val taskInfo = createTestTaskInfo()
        repository.insertTask(taskInfo)

        // 修改任务状态
        val updatedTask = taskInfo.copy(status = TaskInfo.Status.PROCESSING)

        // 测试更新任务
        val result = repository.updateTask(updatedTask)
        assertTrue(result)

        // 验证任务已更新
        val retrievedTask = repository.getTaskById(taskInfo.id)
        assertEquals(TaskInfo.Status.PROCESSING, retrievedTask?.status)

        // 测试更新不存在的任务
        val nonExistentTask = createTestTaskInfo(id = "non-existent")
        val nonExistentResult = repository.updateTask(nonExistentTask)
        assertFalse(nonExistentResult)
    }

    @Test
    fun `test getTaskById`() = runBlocking {
        // 创建并插入测试任务
        val taskInfo = createTestTaskInfo()
        repository.insertTask(taskInfo)

        // 测试获取存在的任务
        val retrievedTask = repository.getTaskById(taskInfo.id)
        assertEquals(taskInfo, retrievedTask)

        // 测试获取不存在的任务
        val nonExistentTask = repository.getTaskById("non-existent")
        assertNull(nonExistentTask)
    }

    @Test
    fun `test getAllTasks`() = runBlocking {
        // 清除现有任务
        // (在实际应用中，可能需要额外的清理方法)

        // 创建并插入多个测试任务
        val task1 = createTestTaskInfo(id = "task1")
        val task2 = createTestTaskInfo(id = "task2")
        repository.insertTask(task1)
        repository.insertTask(task2)

        // 测试获取所有任务
        val allTasks = repository.getAllTasks()
        assertEquals(2, allTasks.size)
        assertTrue(allTasks.contains(task1))
        assertTrue(allTasks.contains(task2))
    }

    @Test
    fun `test getTasksByStatus`() = runBlocking {
        // 清除现有任务
        // (在实际应用中，可能需要额外的清理方法)

        // 创建并插入不同状态的测试任务
        val pendingTask = createTestTaskInfo(id = "pending", status = TaskInfo.Status.PENDING)
        val processingTask = createTestTaskInfo(id = "processing", status = TaskInfo.Status.PROCESSING)
        val completedTask = createTestTaskInfo(id = "completed", status = TaskInfo.Status.COMPLETED)

        repository.insertTask(pendingTask)
        repository.insertTask(processingTask)
        repository.insertTask(completedTask)

        // 测试按状态获取任务
        val pendingTasks = repository.getTasksByStatus(TaskInfo.Status.PENDING)
        assertEquals(1, pendingTasks.size)
        assertEquals(pendingTask, pendingTasks[0])

        val processingTasks = repository.getTasksByStatus(TaskInfo.Status.PROCESSING)
        assertEquals(1, processingTasks.size)
        assertEquals(processingTask, processingTasks[0])

        val completedTasks = repository.getTasksByStatus(TaskInfo.Status.COMPLETED)
        assertEquals(1, completedTasks.size)
        assertEquals(completedTask, completedTasks[0])
    }

    @Test
    fun `test deleteTask`() = runBlocking {
        // 创建并插入测试任务
        val taskInfo = createTestTaskInfo()
        repository.insertTask(taskInfo)

        // 测试删除任务
        val result = repository.deleteTask(taskInfo.id)
        assertTrue(result)

        // 验证任务已删除
        val deletedTask = repository.getTaskById(taskInfo.id)
        assertNull(deletedTask)

        // 测试删除不存在的任务
        val nonExistentResult = repository.deleteTask("non-existent")
        assertFalse(nonExistentResult)
    }

    // 辅助方法：创建测试用的TaskInfo对象
    private fun createTestTaskInfo(
        id: String = UUID.randomUUID().toString(),
        status: TaskInfo.Status = TaskInfo.Status.PENDING
    ): TaskInfo {
        val videoFile = VideoFile(
            id = UUID.randomUUID().toString(),
            name = "test_video.mp4",
            path = "/storage/emulated/0/Download/test_video.mp4",
            duration = 10000,
            size = 5242880,
            format = "mp4"
        )

        val audioParam = AudioParam()
        val createTime = Date()

        return TaskInfo(
            id = id,
            videoFile = videoFile,
            audioParam = audioParam,
            status = status,
            createTime = createTime
        )
    }
}